/*
 * Copyright (c) 2016 www.smarthome.com All rights reserved. 本软件源代码版权归----所有,未经许可不得任意复制与传播.
 */
package com.smarthome.base.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShOrdersDao;
import com.smarthome.base.dao.base.BaseDao;
import com.smarthome.base.service.ShOrdersService;
import com.smarthome.base.service.base.BaseServiceImpl;
import com.smarthome.domain.bean.ShOrders;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * 
 * @ClassName: ShOrdersOfSendCourierServiceImpl
 * @Description: ShOrdersService 实现类（代发快递）
 * @author cyj
 * @date 2016年3月24日 下午12:11:12
 *
 */
@Service("shOrdersOfSendCourierService")
public class ShOrdersOfSendCourierServiceImpl extends
		BaseServiceImpl<ShOrders, Long> implements ShOrdersService {

	@Resource
	private ShOrdersDao shOrdersOfSendCourierDao;

	public BaseDao<ShOrders, Long> getDao() {
		return shOrdersOfSendCourierDao;
	}

	@Override
	public DataGrid queryShOrdersPage(ShOrders shOrders, PageHelper page) {
		DataGrid dataGrid = new DataGrid();
		if (!StringUtils.isBlank(page.getSort())) {
			shOrders.setOrderField(page.getSort());
		}
		if (!StringUtils.isBlank(page.getOrder())) {
			shOrders.setOrderFieldType(page.getOrder());
		}

		Integer totalCount = shOrdersOfSendCourierDao
				.queryShOrderCount(shOrders);
		dataGrid.setTotal(totalCount.longValue());
		int startIndex = (page.getPage() - 1) * page.getRows();
		int endIndex = page.getPage() * page.getRows();
		if (totalCount > 0 && totalCount > startIndex) {
			shOrders.setStartIndex(startIndex);
			shOrders.setEndIndex(endIndex);
			List<ShOrders> shOrdersList = shOrdersOfSendCourierDao
					.queryShOrdersListPage(shOrders);
			dataGrid.setRows(shOrdersList);
		}
		return dataGrid;
	}

	/**
	 * 未实现
	 */
	@Override
	public int batchUpdate(ShOrders shOrders) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<ShOrders> getShOrdersList(ShOrders shOrders) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ShOrders getDetailById(Long id) {
		if(id == null) {
			return null;
		}
		return shOrdersOfSendCourierDao.getDetailById(id);
	}

	@Override
	public List<ShOrders> getSendCourierDetailList(ShOrders shOrders) {
		return shOrdersOfSendCourierDao.queryShOrdersListPage(shOrders);
	}

	@Override
	public Integer getSendCourierCount(ShOrders shOrders) {
		return shOrdersOfSendCourierDao.queryShOrderCount(shOrders);
	}

	@Override
	public Map<String, Object> getCountAll(ShOrders shOrders) {
		return shOrdersOfSendCourierDao.getCountAll(shOrders);
	}

	@Override
	public Map<String, Object> getPicCount(ShOrders shOrders) {
		String[] months = getMonths();
		shOrders.addQueryData("startGmtCreate", months[0] + "-01");
		shOrders.addQueryData("endGmtCreate", addMonth(new Date(), 1) + "-01");
		Map<String, Object> map = new HashMap<String, Object>();
		shOrders.setOrderType(1);
		Object fee1 = shOrdersOfSendCourierDao.getCountAll(shOrders).get("fee");
		shOrders.setOrderType(2);
		Object fee2 = shOrdersOfSendCourierDao.getCountAll(shOrders).get("fee");
		shOrders.setOrderType(3);
		Object fee3 = shOrdersOfSendCourierDao.getCountAll(shOrders).get("fee");
		List<Integer> count1 = getCountNum(months, 1, shOrders.getResidenceId());
		List<Integer> count2 = getCountNum(months, 2, shOrders.getResidenceId());
		List<Integer> count3 = getCountNum(months, 3, shOrders.getResidenceId());
		map.put("fee1", fee1);
		map.put("fee2", fee2);
		map.put("fee3", fee3);
		map.put("count1", count1);
		map.put("count2", count2);
		map.put("count3", count3);
		map.put("months", months);
		return map;
	}

	public List<Integer> getCountNum(String[] months, int type, Long residenceId) {
		List<Integer> count1 = new ArrayList<Integer>();
		ShOrders shOrders = new ShOrders();
		shOrders.setResidenceId(residenceId);
		shOrders.setOrderType(type);
		for (int i = 0; i < months.length; i++) {
			shOrders.addQueryData("startGmtCreate", months[i] + "-01");
			shOrders.addQueryData("endGmtCreate", addMonth(months[i], 1) + "-01");
			count1.add(Integer.parseInt(shOrdersOfSendCourierDao.getCountAll(shOrders)
					.get("count").toString()));
		}
		return count1;
	}

	private String[] getMonths() {
		Date date = new Date();
		String[] months = { addMonth(date, -5),
				addMonth(date, -4), addMonth(date, -3), addMonth(date, -22),
				addMonth(date, -1), addMonth(date, 0) };
		return months;
	}

	private String addMonth(Date date, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, days);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		return sdf.format(cal.getTime());
	}

	private String addMonth(String date, int days) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.add(Calendar.MONTH, days);
		return sdf.format(cal.getTime());
	}

}